<?php
namespace Home\Controller;

use Common\Controller\HomeController;
use Common\Model\GoodsModel;
use Org\Util\Cart;

/**
 * 首页管理控制器
 * Class IndexController
 * @package Home\Controller
 */
class CartController extends HomeController
{
	//首页
	public function index ()
	{
		//分配公共头部数据
		$conf = ['js'=>['list'],
			'css'=>['index']
		];
		$this->assign('conf',$conf);

//		$dd = (new Cart())->getOrderId();
//		dd($dd);
//		$ddd = (new Cart())->getTotalNums();
//		dd($ddd);
//		$dddd = (new Cart())->getTotalPrice();
//		dd($dddd);
//		$ddddd = (new Cart())->getAllData();
//		dd($ddddd);
//		dd($_SESSION);
		$data = $_SESSION['cart'] ? $_SESSION['cart'] : [];
		//循环处理数据亚茹图片
		foreach($data['goods'] as $k=>$v){
			$data['goods'][$k]['pic'] = (new GoodsModel())->where("gid={$v['id']}")->getField('pic');
		}
		dd($data);

		$this->assign('data',$data);
		$this->display();
	}
	/**
	 * 异步修改购物车数据
	 */
	public function ajaxUpdateCart()
	{
		if(IS_AJAX){
//			dd($_SESSION);
//			dd($_POST);die;
			$data=array(
				'sid'=>$_POST['sid'],// 唯一 sid，添加购物车时自动生成
				'num'=>$_POST['num']
			);
			(new Cart())->update($data);
		}
	}
}